﻿using Direct.IDAL.User.Jurisdiction;
using DirectSell.Common.Filter;
using DirectSell.Common.Log;
using DirectSell.Common.Page;
using DirectSell.Common.Session;
using DirectSell.Common.Url;
using DirectSell.IBLL;
using DirectSell.Model.User;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.WebLayer.Areas.User.Controllers
{
    /// <summary>
    /// 会员激活
    /// </summary>
    public class ActivationController : Controller
    {

        #region 注入
        [Dependency("UserActivation")]
        protected IOperation _activation { get; set; }

        [Dependency]
        protected IActivationRepository _dalactivation { get; set; }

        [Dependency]
        protected IModifyInfoRespository modifydal { get; set; }
        #endregion

        #region 首页
        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        [LoginFilter(IsCheck =true)]
        public async Task<ActionResult> Index(int? page)
        {
            var session = SessionInfo.GetSession();
            ViewBag.Balance =string.Format("{00:f2}",_dalactivation.GetBalance(session.UserId));
            var list = await _dalactivation.NotActive(session.UserCode,page??1, CommonConfig.PageSize);
            return View(list);
        }
        #endregion

        #region 分部视图
        /// <summary>
        /// 分部视图
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public async Task<ActionResult> List(int? page)
        {
            var session = SessionInfo.GetSession();
            ViewBag.Balance = string.Format("{00:f2}", _dalactivation.GetBalance(session.UserId));
            var list = await _dalactivation.NotActive(session.UserCode, page ?? 1, CommonConfig.PageSize);
            //var pagedList = list.PageList(page);
            return PartialView(list);
        }
        #endregion

        #region 会员激活操作
        /// <summary>
        /// 会员激活
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public async Task<UserRespCode> Activation(int UserId)
        {
            ActivationReq model = new ActivationReq()
            {
                ActivationId = SessionInfo.GetSession().UserId,
                UserId = UserId
            };

            var Balance = _dalactivation.GetBalance(model.ActivationId);

            var NeedMoney = _dalactivation.GetNeedMoney(model.UserId);

            if (Balance < NeedMoney)
            {
                return UserRespCode.余额不足;
            }

            return  await _activation.DoOperationAsync(model);
        }
        #endregion

        #region 会员删除
        /// <summary>
        /// 会员删除
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public UserRespCode Delete(int UserId)
        {
            MyLog.Log(UserId + "|"+ SessionInfo.GetSession().UserCode, "会员删除", "会员删除", Model.Log.MyLogLevel.Info);
            if (!_dalactivation.Delete(UserId))
            {
                return UserRespCode.失败;
            }
            return UserRespCode.成功;
        }
        #endregion


    }
}